package org.aom.litebean.module.menu;



import java.util.Map;

import org.aom.entity.menu.Menu;
import org.aom.litebean.module.base.BaseEntityFormBean;
import org.aom.service.menu.IMenuManager;
import org.aom.util.FaceUtil;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.ManagedProperty;
import org.operamasks.faces.annotation.Required;

@ManagedBean(name = "module.menu.menuFormBean", scope = ManagedBeanScope.REQUEST)
public class MenuFormBean extends BaseEntityFormBean{
	
	@Bind(id = "menuName", attribute = "value")
    @Required(message = "#{this.getMessages().get('required')}")
	private String menuName;
	
	
	private Menu menu;
	
	@ManagedProperty("#{menuManager}")
	private IMenuManager menuManager;
	
	@Action(id="ok")
	public void ok(){
		Map<String, Object> session = FaceUtil.getSessionMap();
		if(session.get(ACTION_NAME).toString().equals("add")){
			menu = new Menu();
		}else {
			selectedModuleId = (Long)session.get(SELECTED_MODULE_ID);
			menu = menuManager.get(selectedModuleId);
		}
		menu.setMenuName(menuName);
		menuManager.save(menu);
		cancel();
	}
	
	@Override
	protected void processCancel(){
		menuName = "";
	}
	
	
	@BeforeRender
	protected void beforeRender(boolean isPostBack) {
		initBaseResource();	
		Map<String, Object> session = FaceUtil.getSessionMap();
		if (session.get(ACTION_NAME).toString().equals("mod")) {
			selectedModuleId = (Long)session.get(SELECTED_MODULE_ID);
			menu = menuManager.get(selectedModuleId);
			menuName = menu.getMenuName();
		}
	}
}
